package org.jspresso.framework.application.frontend.action.entity.lov;

import java.util.Map;

import org.jspresso.framework.action.IAction;
import org.jspresso.framework.action.IActionHandler;
import org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController;
import org.jspresso.framework.application.frontend.action.lov.OkLovAction;
import org.jspresso.framework.model.entity.IEntity;
import org.jspresso.model.ActionTestIo;
import org.jspresso.model.TestEntity;
import org.jspresso.model.TestScope;
import org.jspresso.tester.utils.TestEntitySerializer;

public class OkChoosenEntityLovAction<E,F,G> extends OkLovAction<E,F,G> {
	IAction viewConnectorSetAction;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean execute(IActionHandler actionHandler, Map context) {
		super.execute(actionHandler, context);
		ActionTestIo parentIo = (ActionTestIo)getParentModel(context);
		TestScope scope = parentIo.getBackActionTest().getTestScope();
		
		IEntity entity = (IEntity) getActionParameter(context);
		TestEntitySerializer.serializeToStore(entity, scope, false, false, (HibernateBackendController) getBackendController(context));
		TestEntity testEntity = TestEntitySerializer.findTestEntity(entity.getId(), entity.getComponentContract(), scope, (HibernateBackendController) getBackendController(context));
		setActionParameter(testEntity, context);

		if(viewConnectorSetAction != null) {
			return actionHandler.execute(viewConnectorSetAction, context);
		}
		else {
			return super.execute(actionHandler, context);
		}
	}

	public void setViewConnectorSetAction(IAction viewConnectorSetAction) {
		this.viewConnectorSetAction = viewConnectorSetAction;
	}

	@Override
	public IAction getNextAction(Map<String, Object> context) {
		return null;
	}


}
